<?php

/**
 * TextosController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class GruposController extends Zend_Controller_Action {
	
	private $usuario;
	private $gruposDAO;
	private $perfil;
	
	protected $_acl;
	protected $_session;
	
	public function init() {
		$config = Zend_Registry::get ( 'config' );
		$this->_acl = $this->getFrontController ()->getParam ( 'acl' );
		$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
		$this->usuario = $this->_session->storage;
		$this->gruposDAO = new Grupos ( );
		
		if (! isset ( $this->usuario )) {
			$this->perfil = "visitante";
		} else {
			$this->perfil = $this->usuario->perfil;
		}
	
	}
	
	public function preDispatch() {
		if (! Zend_Auth::getInstance ()->hasIdentity ()) {
			$this->view->logado = 0;
			
		} else {
			$config = Zend_Registry::get ( 'config' );
			$this->_session = new Zend_Session_Namespace ( $config->session->namespace );
			$this->_usuario = $this->_session->storage;
			$this->view->logado = 1;
		}
	
	}
	
	public function indexAction() {
		
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
		$busca = $params->busca;
		$areas = $params->areas;
		$page = $params->page;
		
		if (isset ( $busca ) || (isset ( $areas ) && $areas != 0)) {
			$page = 1;
		} else if (! isset ( $page ) && $this->_session->page) {
			$page = $this->_session->page;
		} else {
			$this->_session->page = $page;
		}
		
		if (! isset ( $busca )) {
			$busca = $this->_session->busca;
		} else {
			$this->_session->busca = $busca;
		}
		
		$this->view->busca = $busca;
		
		if (! isset ( $areas )) {
			$areas = $this->_session->areas;
		} else {
			$this->_session->areas = $areas;
		}
		
		$this->view->area = $areas;
		
		/*if ($busca || $areas)
			$grupos = $this->textosDAO->search ( $busca, $areas ); 
		else*/
		$grupos = $this->textosDAO->findAll ( 'data desc' );
		
		$this->view->grupos = $grupos;
		
		$paginator = Zend_Paginator::factory ( $grupos );
		
		$paginator->setItemCountPerPage ( 10 );
		$paginator->setCurrentPageNumber ( $page );
		
		$paginator->setPageRange ( range ( 1, 5 ) );
		Zend_View_Helper_PaginationControl::setDefaultViewPartial ( "_partials/paginacao.phtml" );
		
		$this->view->paginator = $paginator;
				
		$this->view->perfil = $this->perfil;
		
		$areas = new Areas ( );
		$this->view->areas = $areas->fetchAll ();
	
	}

	
}
